package com.zjdiepu.www.biz.partner;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.ThirdLogEventStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.ThirdModeIDType;
import com.zjdiepu.www.dao.partner.ThLogsMapper;
import com.zjdiepu.www.dao.partner.ThLogsSubMapper;
import com.zjdiepu.www.ibiz.partner.IThLogsService;
import com.zjdiepu.www.model.partner.dto.ThLogs;
import com.zjdiepu.www.model.partner.dto.ThLogsSub;
import com.zjdiepu.www.model.partner.ext.ThLogsExt;

/**
 * Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年09月21日 10时04分<br/>
 * @since 1.0<br/>
 */
@Service
public class ThLogsServiceImpl extends BaseService implements IThLogsService {
	public static final Logger logger = LoggerFactory.getLogger(ThLogsServiceImpl.class);

	@Autowired
	private ThLogsMapper thLogsMapper;
	@Autowired
	private ThLogsSubMapper thLogsSubMapper;
	
	@Override
	public String[] saveOrUpdateLog(String busiId, String dataId, String busiType,
			String xeApplyNo,String requestMessageEvent, String responseMessageEvent,String requestMessageEventEncode,
			ThirdLogEventStatus thirdLogEventStatus, String describeEvent,
			ThirdModeIDType ThModeIDType, String currentUserId,
			String currentUserName, String... remarks) {
		ThLogs thLogs = new ThLogs();
		thLogs.initInsertData(currentUserId, currentUserName);
		thLogs.setDataId(dataId);
		thLogs.setBusiType(busiType);
		thLogs.setBusiId(busiId);
		thLogs.setThirdId(xeApplyNo);
		if(null != ThModeIDType) {
			thLogs.setThirdModeId(ThModeIDType.getCode());
			thLogs.setThirdModeName(ThModeIDType.getValue());
		}
		thLogs.setDescribeEvent(describeEvent);
		if(null != thirdLogEventStatus){
			thLogs.setStatusEvent(thirdLogEventStatus.getCode());
		}
		thLogsMapper.insert(thLogs);
		
		ThLogsSub thLogsSub = new ThLogsSub();
		thLogsSub.initInsertData(currentUserId, currentUserName);
		thLogsSub.setThLogsId(thLogs.getId());
		thLogsSub.setDataId(dataId);
		thLogsSub.setBusiType(busiType);
		thLogsSub.setBusiId(busiId);
		thLogsSub.setThirdId(xeApplyNo);
		if(null != ThModeIDType) {
			thLogsSub.setThirdModeId(ThModeIDType.getCode());
			thLogsSub.setThirdModeName(ThModeIDType.getValue());
		}
		thLogsSub.setRequestMessageEvent(requestMessageEvent);
		thLogsSub.setResponseMessageEvent(responseMessageEvent);
		thLogsSub.setRequestMessageEventEncode(requestMessageEventEncode);
		if(remarks != null && remarks.length > 0) {
			thLogsSub.setRemarks(remarks[0]);
		}
		thLogsSubMapper.insert(thLogsSub);
		return new String[]{thLogs.getId(), thLogsSub.getId()};
	}
	
	@Override
	public void saveUpdateAndDelete(ThLogs thLogs, OperateType operType) {
		switch (operType) {
		case add:
			thLogs.initInsertData(thLogs.getModifier(), thLogs.getModifierName());
			thLogsMapper.insert(thLogs);
			break;
		case edit:
			thLogs.initUpdateData(thLogs.getModifier(), thLogs.getModifierName());
			// 影响的记录数
			int updateCount = thLogsMapper.updateByPrimaryKeySelective(thLogs);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			thLogs.initDeleteData(thLogs.getModifier(), thLogs.getModifierName());
			// 影响的记录数
			int deleteCount = thLogsMapper.updateByPrimaryKeySelective(thLogs);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ThLogs> selectByExt(ThLogsExt thLogsExt) {
		return thLogsMapper.selectByExt(thLogsExt);
	}
	
	@Override
	public int countByExt(ThLogsExt thLogsExt){
		return thLogsMapper.countByExt(thLogsExt);
	}
	
	@Override
	public ThLogs selectById(String id) {
		return thLogsMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			thLogsMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ThLogs thLogs) {
		return thLogsMapper.countRefined(thLogs);
	}

	@Override
	public ThLogs selectRefined(ThLogs thLogs) {
		return thLogsMapper.selectRefined(thLogs);
	}
	
	@Override
	public int deletedRefined(ThLogs thLogs) {
		return thLogsMapper.deletedRefined(thLogs);
	}

	@Override
	public boolean repeat(ThLogs thLogs) {
		int count = countRefined(thLogs);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(thLogs.getId())) {
			return true;
		}
		ThLogs dbItem = selectRefined(thLogs);
		if(null == dbItem) {
			return false;
		}
		return !thLogs.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return thLogsMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<ThLogs> thLogss) {
		return thLogsMapper.insertBatch(thLogss);
	}
}
